package myauctioneer.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import myauctioneer.App;
import myauctioneer.dto.RatingDetail;

public class RatingDetailDAO implements RatingDetailDAOI {

	private PreparedStatement saveStmt;
	private PreparedStatement loadStmt;
	private PreparedStatement loadStmt1;

	public RatingDetailDAO() {
		try {
			saveStmt = App
					.getDb()
					.prepareStatement(
							"INSERT INTO rating_detail(rating_detail_id, labeling, note, time) "
									+ "VALUES (?, ?, ?, to_date(?, 'dd-mm-yyyy HH24:MI'))");
			loadStmt = App.getDb().prepareStatement(
					"SELECT * FROM rating_detail "
							+ "WHERE rating_detail_id = ?");
			loadStmt1 = App.getDb().prepareStatement(
					"SELECT * FROM rating_detail "
							+ "WHERE labeling = ? AND note = ?");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void saveRatingDetail(RatingDetail rD) {
		try {
			saveStmt.setInt(1, rD.getRatingDetailId());
			saveStmt.setString(2, rD.getLabeling());
			saveStmt.setString(3, rD.getNote());
			saveStmt.setString(4, rD.getTime());
			saveStmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public RatingDetail loadRatingDetailByRatingDetailId(int ratingDetailId) {
		RatingDetail rD = new RatingDetail();
		try {
			loadStmt.setInt(1, ratingDetailId);
			ResultSet result = loadStmt.executeQuery();
			if (!result.next())
				return null;

			rD.setRatingDetailId(result.getInt("rating_detail_id"));
			rD.setLabeling(result.getString("labeling"));
			rD.setNote(result.getString("note"));
			rD.setTime(result.getString("time"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return rD;
	}

	@Override
	public RatingDetail loadRatingDetailByLabelingNote(String labeling,
			String note) {
		RatingDetail rD = new RatingDetail();
		try {
			loadStmt1.setString(1, labeling);
			loadStmt1.setString(2, note);
			ResultSet result = loadStmt1.executeQuery();
			if (!result.next())
				return null;

			rD.setRatingDetailId(result.getInt("rating_detail_id"));
			rD.setLabeling(result.getString("labeling"));
			rD.setNote(result.getString("note"));
			rD.setTime(result.getString("time"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return rD;
	}

	@Override
	public ArrayList<RatingDetail> loadRatingsDetailsByRatingDetailId(
			int ratingDetailId) {
		ArrayList<RatingDetail> rDAL = new ArrayList<RatingDetail>();
		try {
			loadStmt.setInt(1, ratingDetailId);
			ResultSet result = loadStmt.executeQuery();

			while(result.next())
				rDAL.add(this.loadRatingDetailByRatingDetailId(result.getInt("rating_detail_id")));
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rDAL;
	}

}
